package com.gitee.wsl.time.calendar

import com.gitee.wsl.time.plus
import com.gitee.wsl.time.timestamp
import com.gitee.wsl.time.toLocalDate
import kotlin.time.Duration.Companion.days


/**
 *
 *
 * This implementation of the Calendar excludes a set of days of the month. You may use it to exclude every first day of
 * each month for example. But you may define any day of a month.
 *
 *
 * @author Juergen Donnerstag
 * @see ICalendar
 *
 * @see BaseCalendar
 */
class MonthlyCalendar(baseCalendar: Calendar?=null) : AbstractCalendar(baseCalendar){
    // An array to store a months days which are to be excluded.
    // java.util.Calendar.get( ) as index.
    private var excludeDays = BooleanArray(MAX_DAYS_IN_MONTH)

    // Will be set to true, if all week days are excluded
    private var excludeAll = false


    init {
        // all days are included by default
        excludeAll = areAllDaysExcluded()
    }

    var daysExcluded: BooleanArray
        /**
         *
         *
         * Get the array which defines the exclude-value of each day of month. Only the first 31 elements of the array are
         * relevant, with the 0 index element representing the first day of the month.
         *
         */
        get() = excludeDays
        /**
         *
         *
         * Redefine the array of days excluded. The array must non-null and of size greater or equal to 31. The 0 index
         * element represents the first day of the month.
         *
         */
        set(days) {
            requireNotNull(days) { "The days parameter cannot be null." }

            require(days.size >= MAX_DAYS_IN_MONTH) { "The days parameter must have a length of at least " + MAX_DAYS_IN_MONTH + " elements." }

            excludeDays = days
            excludeAll = areAllDaysExcluded()
        }

    /**
     *
     *
     * Return true, if day is defined to be excluded.
     *
     *
     * @param day The day of the month (from 1 to 31) to check.
     */
    fun isDayExcluded(day: Int): Boolean {
        require(!((day < 1) || (day > MAX_DAYS_IN_MONTH))) { "The day parameter must be in the range of 1 to $MAX_DAYS_IN_MONTH" }

        return excludeDays[day - 1]
    }

    /**
     *
     *
     * Redefine a certain day of the month to be excluded (true) or included (false).
     *
     *
     * @param day The day of the month (from 1 to 31) to set.
     */
    fun setDayExcluded(day: Int, exclude: Boolean) {
        require(!((day < 1) || (day > MAX_DAYS_IN_MONTH))) { "The day parameter must be in the range of 1 to " + MAX_DAYS_IN_MONTH }

        excludeDays[day - 1] = exclude
        excludeAll = areAllDaysExcluded()
    }

    /**
     *
     *
     * Check if all days are excluded. That is no day is included.
     *
     */
    fun areAllDaysExcluded(): Boolean {
        for (i in 1..MAX_DAYS_IN_MONTH) {
            if (!isDayExcluded(i)) {
                return false
            }
        }

        return true
    }

    /**
     *
     *
     * Determine whether the given time (in milliseconds) is 'included' by the Calendar.
     *
     *
     *
     *
     * Note that this Calendar is only has full-day precision.
     *
     */
    override fun isTimeIncluded(timeStamp: Long): Boolean {
        if (timeStamp <= 0) return false

        if (excludeAll) {
            return false
        }

        // Test the base calendar first. Only if the base calendar not already
        // excludes the time/date, continue evaluating this calendar instance.
        if (!super.isTimeIncluded(timeStamp)) {
            return false
        }

        //val cl: Calendar = createJavaCalendar(timeStamp)
        //val day: Int = cl.get(java.util.Calendar.DAY_OF_MONTH)

        val day: Int = timeStamp.toLocalDate().dayOfMonth

        return !(isDayExcluded(day))
    }

    /**
     *
     *
     * Determine the next time (in milliseconds) that is 'included' by the Calendar after the given time. Return the
     * original value if timeStamp is included. Return 0 if all days are excluded.
     *
     *
     *
     *
     * Note that this Calendar is only has full-day precision.
     *
     */
    override fun getNextIncludedTime(timeStamp: Long): Long {
        var timeStamp = timeStamp
        if (excludeAll) {
            return 0
        }

        // Call base calendar implementation first
        val baseTime: Long = super.getNextIncludedTime(timeStamp)
        if ((baseTime > 0) && (baseTime > timeStamp)) {
            timeStamp = baseTime
        }

        // Get timestamp for 00:00:00
        //val cl: java.util.Calendar = getStartOfDayJavaCalendar(timeStamp)
        //var day: Int = cl.get(java.util.Calendar.DAY_OF_MONTH)
        var dateTime = timeStamp.toLocalDate()
        var day: Int = dateTime.dayOfMonth

        if (!isDayExcluded(day)) {
            return timeStamp // return the original value
        }

        while (isDayExcluded(day)) {
            //cl.add(java.util.Calendar.DATE, 1)
            //day = cl.get(java.util.Calendar.DAY_OF_MONTH)
            dateTime = (dateTime + 1.days).date
            day = dateTime.dayOfMonth
        }

        return dateTime.timestamp
    }

    companion object {
        const val serialVersionUID: Long = 419164961091807944L

        private const val MAX_DAYS_IN_MONTH = 31
    }
}
